import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STChange, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-recharge-record',
  templateUrl: './recharge-record.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class RechargeRecordComponent extends BasicTableComponent implements OnInit {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  @ViewChild('remarkodal', { static: true })
  remarkodal!: any;
  rechargeRemark = '';

  constructor(public service: FreightAccountService, private modal: NzModalService, public searchDrawerService: SearchDrawerService) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf?.value });
      if (this.sf?.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf?.value.createTime?.[0] || '',
            end: this.sf?.value.createTime?.[1] || ''
          }
        });
      }
    }
    return requestOptions;
  };

  addRemark(item: any) {
    this.rechargeRemark = item.remark;
    const modal = this.modal.create({
      nzTitle: this.rechargeRemark ? '修改备注' : '添加备注',
      nzContent: this.remarkodal,
      nzFooter: [
        {
          type: 'primary',
          label: '确认',
          loading: () => this.service.http.loading,
          onClick: () => {
            this.service.request(this.service.$api_edit_remark, { id: item.id, remark: this.rechargeRemark }).subscribe(res => {
              if (res) {
                this.service.msgSrv.success('修改成功');
                this.st.load(1);
                modal.destroy();
              }
            });
            return false;
          }
        }
      ]
    });
  }

  goBack() {
    history.go(-1);
  }

  exportList() {
    this.service.exportStart(
      {
        ...this.sf?.value,
        createTime: {
          start: this.sf?.value.createTime?.[0] || '',
          end: this.sf?.value.createTime?.[1] || ''
        },
        pageSize: -1
      },
      this.service.$api_get_exportPageByOperator
    );
  }

  private initSF(): SFSchema {
    return {
      properties: {
        rechargeNo: {
          type: 'string',
          title: '充值单号',
          ui: {
            placeholder: '请输入'
          }
        },
        rechargeStatus: {
          type: 'string',
          title: '充值状态',
          enum: [
            { label: '全部', value: '' },
            { label: '充值中', value: '1' },
            { label: '充值失败', value: '2' },
            { label: '充值成功', value: '3' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        createTime: {
          title: '充值时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true
          } as SFDateWidgetSchema
        },
        roleName: {
          type: 'string',
          title: '账户名称',
          ui: {
            placeholder: '请输入',
            autocomplete: 'off'
          }
        },
        accountType: {
          type: 'string',
          title: '账户类型',
          enum: [
            { label: '全部', value: '' },
            { label: '货主账户', value: '1' },
            { label: '司机账户', value: '2' },
            { label: '营商商账户', value: '3' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: '' },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '充值时间', index: 'createTime', type: 'date', width: 180 },
      { title: '充值单号', index: 'rechargeNo', width: 140 },
      { title: '网络货运人', index: 'ltdName', width: 160 },
      { title: '银行类型', index: 'bankTypeLabel', width: 100 },
      { title: '账户类型', index: 'accountTypeLabel', width: 100 },
      { title: '账户名称', index: 'roleName', width: 160 },
      { title: '虚拟账户', index: 'virtualAccount', width: 100 },
      {
        title: '充值金额',
        index: 'rechargeAmount',
        width: 160,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.rechargeAmount }) }
      },
      { title: '充值银行账户', render: 'transferBankAccount', width: 200 },
      { title: '充值方式', index: 'payChannelLabel', width: 100 },
      { title: '充值状态', index: 'rechargeStatusLabel', width: 100 },
      { title: '银行流水号', index: 'paySerialNumber', width: 120 },
      {
        title: '操作',
        width: 120,
        fixed: 'right',
        className: 'text-center',
        buttons: [
          { type: 'divider' },
          {
            text: '查看回单<br>',
            acl: { ability: ['FINANCIAL-RECHARGE-receiptApply'] },
            click: item =>
              this.service.getReceiptUrl(item.receiptUrl, {
                bankType: item.bankType,
                rmYll: item.roleId,
                snglFlgCd: item.paySerialNumber2,
                bussType: '05',
                ltdId: item.ltdId,
                accountType: item.accountType
              })
          },
          {
            text: '添加备注',
            acl: { ability: ['FINANCIAL-RECHARGE-addRemark'] },
            click: item => this.addRemark(item)
          }
        ]
      }
    ];
  }
}
